package com.rrd.hive.udf;

import com.weicai.caesar.CaesarUtil;
import org.apache.hadoop.hive.ql.exec.Description;
import org.apache.hadoop.hive.ql.exec.UDF;
import org.apache.hadoop.io.Text;

import java.security.NoSuchAlgorithmException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


@Description(
        name = "caesar_blur",
        value = "_FUNC_(str) - get blur str",
        extended = "Example:\n> SELECT _FUNC_('13811360000');\n  138****0000\n> SELECT _FUNC_('6jxdXEKdEo/bHVdtUWshLZmfEBahoIMjHUUtRRjvrJM=');\n  138****0000"
)
public class MexCaesaBlurUDF extends UDF {
    public  static  final  Pattern PID_BANK_MOBILE_PATTERN = Pattern.compile(".*\\d+.*");
    public Text evaluate(String str) throws NoSuchAlgorithmException {
        Text text = new Text();
        if (str == null || str.equals("")) {
            return null;
        }
        if (CaesarUtil.isEncrypted(str)) {
            str = CaesarUtil.decode(str);
        }
        String [] arr = str.split(" ");
        String result="";
        Matcher m = PID_BANK_MOBILE_PATTERN.matcher(str);
        if(arr.length==1 && m.matches() && str.length()>8  ){
            //如果没有空格分隔，则说明是身份证、手机号、银行卡
            if(str.length()>6){
                result=str.substring(0,2)+str.substring(2,str.length()-4).replaceAll("\\w","*")+str.substring(str.length()-4,str.length());
            }else{
                result=str.substring(0,2)+str.substring(2,str.length()).replaceAll("\\w","*");
            }
        }else{
            for(int i=0;i<arr.length;i++){
                if(i<2){
                    result=result+arr[i]+" ";
                }else{
                    result=result+arr[i].replaceAll("\\w","*")+" ";
                }
            }
            result=result.trim();
        }

        text.set(result);

        return text;
    }


    public static void main(String[] args) throws Exception {
//        手机号：保留前两位和后四位
//        身份证号： 保留前两位和后四位
//        银行卡号：保留前两位和后四位
//        用户姓名：单词1 单词2 其他*，用空格区分
        MexCaesaBlurUDF udf = new MexCaesaBlurUDF();
        System.out.println(udf.evaluate("LUCIA ANGELICA wff").toString());


    }
}
